package com.study.lu.thread.producercustomer.oldjdk;

import com.study.lu.thread.producercustomer.Food;

import java.util.ArrayList;
import java.util.List;

/**
 * <b>System：</b>incommonusefactory<br/>
 * <b>Title：</b>zhongjie.java<br/>
 * <b>Description：</b> 对功能点的描述<br/>
 * <b>@author： </b>raoluping<br/>
 * <b>@date：</b>2017/12/20 17:39<br/>
 * <b>@version：</b> 1.0.0.0 <br/>
 * <b>Copyright (c) 2017 ASPire Tech.</b>
 */
public class FoodFactory {
    private List<Food> foods = new ArrayList<>();

    private final static int FOOD_MAX_SIZE = 1;


    public synchronized void customFood() throws InterruptedException {
        while (foods.size() <= 0) {
            try {
                //System.out.println("目前没有食物，需要等待生产者生产");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Food food = foods.get(0);

        foods.remove(food);
        System.out.println("消费者消费了：食物  " + food.getFoodName() + " 数量" + food.getQty());
        //Thread.sleep(1000);
        notify();


    }

    public synchronized void producerFood() throws InterruptedException {
        while (FOOD_MAX_SIZE <= foods.size()) {
            try {
                //System.out.println("厂库存货已达上限。不能再进行生产食物");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Thread.sleep(2000);
        Food food = new Food("苹果", 1111);
        foods.add(food);
        System.out.println("生产者生产了一份食物");
        notifyAll();


    }
}
